/*
 * Copyright 2011 SWM Services GmbH.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package de.swm.commons.mobile.client.widgets;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Element;

import de.swm.commons.mobile.client.SWMMobile;
import de.swm.commons.mobile.client.utils.IsSWMMobileWidgetHelper;
import de.swm.commons.mobile.client.utils.Utils;
import de.swm.commons.mobile.client.validation.IErrorOutputElement;
import de.swm.commons.mobile.client.validation.IHasValidator;
import de.swm.commons.mobile.client.validation.IValidator;
import de.swm.commons.mobile.client.validation.impl.ValidationHelper;

/**
 * Input type for larger text elements.
 * 
 */
public class TextArea extends com.google.gwt.user.client.ui.TextArea implements FocusHandler, BlurHandler,
		KeyUpHandler, IsSWMMobileWidget, IHasValidator {

	private IsSWMMobileWidgetHelper myWidgetHelper = new IsSWMMobileWidgetHelper();
	private final ValidationHelper<String> validationHelper = new ValidationHelper<String>();



	/**
	 * Default constructor.
	 */
	public TextArea() {
		setStyleName(SWMMobile.getTheme().getMGWTCssBundle().getTextBoxCss().textArea());
		addFocusHandler(this);
		addBlurHandler(this);
		addKeyUpHandler(this);
	}



	@Override
	protected void onLoad() {
		super.onLoad();
		myWidgetHelper.checkInitialLoad(this);
	}



	@Override
	public void onFocus(FocusEvent event) {
		this.addStyleName(SWMMobile.getTheme().getMGWTCssBundle().getTextBoxCss().focus());
	}


	@Override
	public void onBlur(BlurEvent event) {
		this.removeStyleName(SWMMobile.getTheme().getMGWTCssBundle().getTextBoxCss().focus());
		if (this.validationHelper.validate(this)) {
			this.addStyleName(SWMMobile.getTheme().getMGWTCssBundle().getTextBoxCss().error());
		} else {
			this.removeStyleName(SWMMobile.getTheme().getMGWTCssBundle().getTextBoxCss().error());
		}
	}



	@Override
	public void onKeyUp(KeyUpEvent event) {
		Element ele = getElement();
		final int extraLineHeight = 15;
		int scrollHeight = ele.getScrollHeight();
		int clientHeight = ele.getClientHeight();
		if (clientHeight < scrollHeight) {
			ele.getStyle().setHeight(scrollHeight + extraLineHeight, Unit.PX);
		}
	}



	@Override
	public void onInitialLoad() {
	}



	@Override
	public void onTransitionEnd() {
	}



	@Override
	public void setSecondaryStyle(String style) {
		myWidgetHelper.setSecondaryStyle(this, style);
	}

	/**
	 * Adds a validator to this Ui-Widget
	 *
	 * @param validatorToAdd
	 *            the validtors to add
	 */
	public void addValidator(IValidator<String> validatorToAdd) {
		this.validationHelper.addValidator(validatorToAdd);
	}



	/**
	 * Add an error output element where error messages are displayed
	 *
	 * @param errorOutputToAdd
	 *            the error output element to add
	 */
	public void addErrorOuptut(IErrorOutputElement errorOutputToAdd) {
		this.validationHelper.addErrorOuptut(errorOutputToAdd);
	}



	/**
	 * Will remove all registered validators.
	 */
	public void removeAllValidators() {
		this.validationHelper.removeAllValidators();
	}

	@Override
	public boolean validate() {
		return !this.validationHelper.validate(this);
	}
}
